package com.zghhost.codedemo.moviedownload;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.zghhost.codedemo.util.IOUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/4/24 12:52
 */
public class Downloader {
    private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("demo-pool-%d").build();
    private static ThreadPoolExecutor executors = new ThreadPoolExecutor(60, 60,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args)throws Exception{

        File file = new File("/Users/wow/Desktop/fulian4list.txt");
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        try{
            String line = null;
            while((line = br.readLine()) != null){
                if(!line.startsWith("#")){
                    addtask(line);
                }
            }

        }catch(Exception e){
            throw e;
        }finally {
            br.close();
            executors.shutdown();
        }

        new Thread(){
            @Override
            public void run() {
                while(true){
                    if(executors.isTerminated()){
                        System.out.println("下载完毕");
                        break;
                    }
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){}
                }
            }
        }.start();
    }

    public static void addtask(String path){
        if(executors.getQueue().size() < 1000){
            executors.execute(new DownloadTask(path));
        }else{
            try{
                Thread.sleep(100);
            }catch (Exception e){}
            addtask(path);
        }
    }

    public static class DownloadTask implements Runnable{
        private static String prefix = "https://cdn.youku-letv.net/20190105/14463_08b43e9d/1000k/hls/";
        private static String localprefix = "/Users/wow/Downloads/fulian4/";

        private String path;
        public DownloadTask(String path){
            this.path = path;
        }

        @Override
        public void run() {
            try{
                URL rl = new URL(prefix+path);
                HttpURLConnection conn = (HttpURLConnection)rl.openConnection();

                String[] patharr = path.split("/");
                File file = new File(localprefix+patharr[patharr.length-1]);
                IOUtil.is2file(conn.getInputStream(),file);
                conn.disconnect();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
